package com.ys.timer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.entity.advanced.AdsReportLog;
import com.ys.entity.advanced.Profiles;
import com.ys.entity.business.TpAmazonSeller;
import com.ys.entity.business.TpAmazonShop;
import com.ys.entity.finance.TcwAccountDetailedForCompany;
import com.ys.entity.finance.TcwElectronicInvoice;
import com.ys.entity.finance.TcwOperateCompanyPrepayment;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.entity.sys.TbStaticConfig;
import com.ys.enume.MarketPlaceEnum;
import com.ys.enume.advanced.ReportTypeEnum;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.mapper.sys.TbStaticConfigMapper;
import com.ys.service.advanced.AdsSpServise;
import com.ys.service.finance.TcwFinancesEventGroupsOpService;
import com.ys.service.log.ApiLogService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.CommonService;
import com.ys.util.ApiHttpUtils;
import com.ys.util.RedisUtil;
import com.ys.util.dhecp.DateUtils;
import com.ys.util.dhecp.StringUtils;
import com.ys.vo.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 每月生成运营电子开票定时器
 */
@Controller
public class TimerTcwElectronicInvoiceDaily {
	private static Logger logger = LoggerFactory.getLogger(TimerTcwElectronicInvoiceDaily.class);

	@Autowired
	private DiySqlMapper diySqlMapper;

	@Autowired
	private CommonApiService commonApiService;


	/**
	 * 每个月1号3点
	 * @throws Exception
	 */
	@Scheduled(cron = "0 0 3 1 * ?")
	public void getElectronicInvoiceDaily() throws Exception{
		getElectronicInvoice();
	}


	public void getElectronicInvoice() throws Exception{
		logger.info("***********每月定时新增运营电子发票开票单getElectronicInvoice，开始***********");

		try {
			//			上个月第一天
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.MONTH, -1);

			Date startTime = calendar.getTime();
			String invoiceMonth = DateUtils.convertDateToTDate(startTime,"yyyy-MM");

			//获取所有的运营公司信息
			List<ThrOperateCompanyApply> thrOperateCompanyApplyList = new ThrOperateCompanyApply().setOperationsAttribute(2).queryList();

			//查询逸博预付款金额
			List<TcwOperateCompanyPrepayment> tcwOperateCompanyPrepaymentList = new TcwOperateCompanyPrepayment().setVyhzhmc("福州逸博供应链有限公司").queryList();
			BigDecimal preAmount = BigDecimal.ZERO;
			for (TcwOperateCompanyPrepayment tcwOperateCompanyPrepayment : tcwOperateCompanyPrepaymentList){
				preAmount = preAmount.add(tcwOperateCompanyPrepayment.getPreAmount());
			}

			List<TcwElectronicInvoice> tcwElectronicInvoiceList = new ArrayList<>();
			BigDecimal priceExcludingTax = BigDecimal.ZERO;
			int i = 0;
			System.out.println(thrOperateCompanyApplyList.size());
			for (ThrOperateCompanyApply thrOperateCompanyApply : thrOperateCompanyApplyList){
				i++;
				System.out.println(i + thrOperateCompanyApply.getVgsbm());
				//查询对应运营公司的头程流水总计
				BigDecimal fjeSum = diySqlMapper.getFjeSum(thrOperateCompanyApply.getVgsbm()).negate();
				String bid = commonApiService.getCommonNextBid(MenuId.TCW_ElectronicInvoice);
				TcwElectronicInvoice tcwElectronicInvoice = new TcwElectronicInvoice();
				tcwElectronicInvoice.setBid(bid);
				tcwElectronicInvoice.setInvoiceMonth(invoiceMonth);
				tcwElectronicInvoice.setVgsbmBuy(thrOperateCompanyApply.getVgsbm());
				tcwElectronicInvoice.setAnnexFile("0个附件");
				tcwElectronicInvoice.setStatus(1);
				tcwElectronicInvoice.setVgsbmSales("C103");
				if (preAmount.compareTo(fjeSum) >= 0){
					tcwElectronicInvoice.setPriceExcludingTax(fjeSum);
					tcwElectronicInvoice.setAmountsTax(BigDecimal.ZERO);
					tcwElectronicInvoice.setIncludedAmountTax(fjeSum);
				}else {
					priceExcludingTax = fjeSum.subtract(preAmount);
					tcwElectronicInvoice.setPriceExcludingTax(priceExcludingTax);
					tcwElectronicInvoice.setAmountsTax(BigDecimal.ZERO);
					tcwElectronicInvoice.setIncludedAmountTax(priceExcludingTax);
				}
				tcwElectronicInvoiceList.add(tcwElectronicInvoice);
			}
			//批量新增
			int tcwElectronicInvoiceListLength = tcwElectronicInvoiceList.size();
			int j = 0;
			while (tcwElectronicInvoiceListLength > 50) {
				diySqlMapper.insetBatchTcwElectronicInvoice(tcwElectronicInvoiceList.subList(j, j + 50));
				j = j + 50;
				tcwElectronicInvoiceListLength = tcwElectronicInvoiceListLength - 50;
			}
			if (tcwElectronicInvoiceListLength > 0) {
				diySqlMapper.insetBatchTcwElectronicInvoice(tcwElectronicInvoiceList.subList(j, j + tcwElectronicInvoiceListLength));
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		logger.info("***********每月定时新增运营电子发票开票单getElectronicInvoice，结束***********");
	}

}
